Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

241
Views
Proceeding conditionally to the next mergeMap? [ rxjs.Observable]

I'd like to convert the my nested Observables with the use of pipe,map,mergeMap, however there are conditions after which I want it to proceed

observableFunction is an Observable that loops through an array, once its finished you can move on to use the manipulated data

observableFunction(par1,par2)
.subscribe((resp) => {
  i++;
 //do stuff, manipulate resp   
  const lastIteration = someArray.length == i;

  if (lastIteration) {
   //do stuff
   observableFunction(par1a, par2a)
      .subscribe((resp) => {
        observableHandleFunction(resp)
          .subscribe((handledData) => {
            h++;
            const lastIterationSub = someNestedArray.length == h;

            //do stuff, manipulate handledData

            if (lastIterationSub) {
              console.log("done");

          });
      });
  }
});

How can I convert so that it does seem more easy to read by using the features of rxjs that are meant to be used in this scenario? Like applying conditions to mergeMap?

observableFunction(par1,par2)
 .pipe(
    mergeMap((res1) => {
       //do stuff
       CONDITION={
         return observableFunction(par1a,par2a)
       }
    }),
    mergeMap((res2) => return observableHandleFunction(res2))
 )
 .subscribe((res3) => {
        h++;
        //do stuff
        const lastIterationSub = someNestedArray.length == h;

        if (lastIterationSub) {
          console.log("done");

        }
});

With a solution something like this its not gonna work with the condition, leading to an error

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

First of all, nesting subscribe blocks is always a bad idea.

If your observableFunction emits single values (rather than a whole array at once) and when its done it emits a complete value, you can use toArray operator. So:

observableFunction(par1,par2)
.pipe(toArray())
// Here you will have an array of items, now you can proceed with something like:

observableFunction(par1,par2)
  .pipe(
    toArray(),
    mergeMap(array => {
    // If neccesary you can do the same here etc
    }))
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!